<?php
declare (strict_types = 1);

namespace app\controller;

use app\common\model\Ad;
use app\common\model\Blog;
use think\facade\Cache;
use think\facade\Db;
use think\facade\View;
use think\Request;

class Blogs extends Common
{

    public function __construct()
    {
        parent::__construct();
        $ad = Ad::where('board_id',7)->select();
        View::assign("ad",$ad);
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {


        $cid = input("get.cid");
        if($cid){
            $where["cate_id"] = $cid;
        }
        $where["status"] =1;


        if(!isset($where)){
            $where = 1;
        }

        $list = \app\common\model\Blog::with("user")
            ->where($where)
            ->order("top DESC,id ASC")
            ->cache(true)
            ->paginate([
                'list_rows'=> 50,
                'query'=> $this->params,
            ]);
        //echo Db::name("article")->getLastSql();die;
        View::assign("list",$list);
        return  view();
    }

    public function create()
    {
        if(\think\facade\Request::isAjax()){
            $d = \think\facade\Request::param();
            if(empty($d["name"])){
                return  json(["code"=>4000,"msg"=>"名称不能空"]);
            }
            if(empty($d["name"])){
                return  json(["code"=>4000,"msg"=>"url不能空"]);
            }

            if(!strstr($d["url"],"http")){
                return  json(["code"=>4000,"msg"=>"请以http开头"]);
            }

            $user = $this->user;
            $d["uid"] = $user["id"];

            $res = Blog::create($d);
            if($res){
                return  json(["code"=>200,"msg"=>"成功"]);
            }else{
                return  json(["code"=>4000,"msg"=>"失败"]);
            }

        }
        return  view();
    }

}
